package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.velocity.app.VelocityEngine;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.ExaminationDAO;
import com.cip.master.model.ExamPaper;
import com.cip.master.model.Examination;
import com.cip.master.service.ExaminationService;

public class ExaminationServiceImpl implements ExaminationService {
	
	private ExaminationDAO examDAO;
	private VelocityEngine velocityEngine;
	
	
	public String getExamGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Examination> examList = getAllExamList();
			
			List<Map<String,Object>> examArrayList = new ArrayList<Map<String,Object>>();
			
			for (Examination exam : examList) {
				Map<String,Object> examObj = new HashMap<String, Object>();
				examObj.put("id", "<a href='/editExam?method=showEditExam&exam.id="+exam.getId()+"' style='text-decoration:underline;color:blue;'>"+exam.getExamName()+"</a>");
				examObj.put("classYearName", exam.getClassYear().getName());
				examObj.put("status", exam.getStatus());
				
				examArrayList.add(examObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, examArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> examArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, examArrayList, createExamGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createExamGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Academic List");
		context.put("gridId", "MY_EXAMLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Id','Class Name','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "id", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "classYearName", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}

	public List<Examination> getAllExamList() throws Exception {
		return examDAO.getAllExamList();
	}

	public Examination getExamById(int id) throws Exception {
		return examDAO.getExamById(id);
	}

	public Examination saveOrUpdateExam(Examination exam) throws Exception {
		return examDAO.saveOrUpdateExam(exam);
	}

	public Examination editExam(Examination exam) throws Exception {
		return examDAO.editExam(exam);
	}
	
	public Examination getExamByName(String name) throws Exception {
		return examDAO.getExamByName(name);
	}
	
	public List<Examination> getAllExamListByClass(String classId) throws Exception{
		return examDAO.getAllExamListByClass(classId);
	}
	
	public List<ExamPaper> getExamPapersByExam(int examId) throws Exception{
		return examDAO.getExamPapersByExam(examId);
	}
	
	
	/**
	 * Getter and Setter
	 */
	public ExaminationDAO getExamDAO() {
		return examDAO;
	}

	public void setExamDAO(ExaminationDAO examDAO) {
		this.examDAO = examDAO;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}	

}
